package atom.core2.j2ee.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * web 资源.
 * 
 * @author aps-mhc aps-zmc修改增加urldomain
 */
public abstract class WebResource
{
	private String urldomain;

	private String url;
	private String param;
	
	/**
	 * 第一个参数名称.
	 * 
	 * @return
	 */
	public static final String PARAM_NAME = "method";

	public String getUrldomain()
    {
    	return urldomain;
    }

	public void setUrldomain(String urldomain)
    {
    	this.urldomain = urldomain;
    }

	public String getUrl()
    {
    	return url;
    }

	public void setUrl(String url)
    {
    	this.url = url;
    }

	public String getParam()
    {
    	return param;
    }

	public void setParam(String param)
    {
    	this.param = param;
    }
	/**
	 * 获取urldomain地址.
	 * 
	 * @return
	 */
	//public abstract String getUrldomain();

	/**
	 * 获取url地址.
	 * 
	 * @return
	 */
	//public abstract String getUrl();

	/**
	 * 获取参数值.
	 * 
	 * @return
	 */
	//public abstract String getParam();

	/**
	 * 将url和param拼接.
	 * 
	 * @return
	 */
	public String getLink()
	{
		String link = getLink(getUrldomain(), getUrl(), getParam());
		//后来加zhaomc 2012-06-06
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
	    Matcher m = p.matcher(link);
		link = m.replaceAll("");
		return link; 
		//return getLink("",getUrl(), getParam());
	}

	/**
	 * 将urlDomain和url和param拼接.
	 * 
	 * @return
	 */
	public static String getLink(String urlDomain, String url, String param)
	{
		if (StringUtils.isEmpty(url))
		{
			return null;
		}

		StringBuffer key = new StringBuffer();
		if (StringUtils.isNotEmpty(urlDomain))
			key.append(urlDomain);
		key.append(url);

		if (StringUtils.isNotEmpty(param))
		{
			key.append("?");

			key.append(PARAM_NAME);
			key.append("=");
			key.append(param);
		}

		return key.toString();
	}

	/**
	 * 将资源列表分发到map中.
	 * 
	 * @param resources
	 * @return (key: urlDomain、url和param拼接的链接, value:WebResource)
	 */
	public static Map<String, WebResource> map(List<WebResource> resources) {
		Map<String,WebResource> map = new HashMap<String,WebResource>();
		
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		for (WebResource res: resources) {
			String link=res.getLink();
		    Matcher m = p.matcher(link);
			link = m.replaceAll("");
			
			map.put(link, res);
		}
		
		return map;
	}

	/**
	 * 由请求参数获取link.
	 * 
	 * @param request
	 * @return
	 */
	// getRequestURI /quickpay/ylinkdemoMainAction1.do
	// getContextPath /quickpay
	// getRequestURL http://127.0.0.1:9090/quickpay/ylinkdemoMainAction1.do
	// substring后 /ylinkdemoMainAction1.do
	public static String getLink(HttpServletRequest request)
	{
		// aps-zmc 修改 多域单点登陆需要domain
		String url = request.getRequestURI().substring(request.getContextPath().length());
		// String url = request.getRequestURL().toString();
		String param = request.getParameter(WebResource.PARAM_NAME);

		if (StringUtils.isNotEmpty(param))
		{
			url = getLink(url, "", param);
		}

		return url;
	}

	// domain+app+res
	public static String getUrl(HttpServletRequest request)
	{
		//原方案只有.do才加上method=XXX
		/*
		String uri = request.getRequestURL().toString();
		if (uri.endsWith(".jsp"))
		{
			return uri;
		}

		if (uri.endsWith(".do"))
		{
			String actionMethod = request.getParameter(WebResource.PARAM_NAME);

			if (StringUtils.isNotEmpty(actionMethod))
			{
				return uri + "?" + PARAM_NAME + "=" + actionMethod;
			}
		}
		*/
		String uri = request.getRequestURL().toString();

		String actionMethod = request.getParameter(WebResource.PARAM_NAME);

		if (StringUtils.isNotEmpty(actionMethod))
		{
			return uri + "?" + PARAM_NAME + "=" + actionMethod;
		}
		return uri;
	}

	// res
	public static String getURI(HttpServletRequest request)
	{
		//原方案只有.do才加上method=XXX 2012-06-02 2012半年过去了
		/*
		String uri = request.getRequestURI().substring(request.getContextPath().length());
		if (uri.endsWith(".jsp"))
		{
			return uri;
		}

		if (uri.endsWith(".do"))
		{
			String actionMethod = request.getParameter(WebResource.PARAM_NAME);

			if (StringUtils.isNotEmpty(actionMethod))
			{
				return uri + "?" + PARAM_NAME + "=" + actionMethod;
			}
		}
		*/
		String uri = request.getRequestURI().substring(request.getContextPath().length());
		String actionMethod = request.getParameter(WebResource.PARAM_NAME);

		if (StringUtils.isNotEmpty(actionMethod))
		{
			return uri + "?" + PARAM_NAME + "=" + actionMethod;
		}
		else
		{
			String actionMethod2 = request.getParameter("action");
			if (StringUtils.isNotEmpty(actionMethod2))
			{
				return uri + "?action=" + actionMethod2;
			}
			
		}
		return uri;
	}

}
